package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Configuration
@RestControllerAdvice//控制器增强
@Slf4j
public class ExceptionCatch {

    //捕获Exception此类异常
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"您的网络异常，请稍后尝试");
    }

    //可预知异常
    @ExceptionHandler(CustomException.class)
    public ResponseResult exception(CustomException e){
        e.printStackTrace();
        log.error("catch CustomException:{}",e.getMessage());
        return ResponseResult.errorResult(e.getAppHttpCodeEnum());
    }
}
